package com.zhouyibike.uu.ui.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.zhouyibike.uu.R;
import com.zhouyibike.uu.config.Config;
import com.zhouyibike.uu.entity.GetCodeResult;
import com.zhouyibike.uu.http.ApiCallback;
import com.zhouyibike.uu.ui.base.BaseActivity;

import java.util.HashMap;
import java.util.Map;

public class ModifyNickNameActivity extends BaseActivity implements View.OnClickListener{
    private ImageButton mBtnBack;
    private TextView mBtnTitle;
    private TextView tv_save;
    private EditText et_nickname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_nick_name);
        initView();
    }

    private void initView() {
        mBtnBack = (ImageButton) findViewById(R.id.bar_btn_back);
        mBtnTitle = (TextView) findViewById(R.id.bar_text_title);
        mBtnTitle.setText("修改昵称");
        mBtnBack.setOnClickListener(this);

        tv_save= (TextView) findViewById(R.id.tv_bar_edit);
        tv_save.setText("保存");
        tv_save.setVisibility(View.VISIBLE);
        tv_save.setOnClickListener(this);

        et_nickname= (EditText) findViewById(R.id.et_modifynickname_nickname);
    }
    private void saveNickName(){
        Map<String, String> params = new HashMap<>();
        params.put("token", preferences.getString("token",""));
        params.put("nickName", et_nickname.getText().toString());
        showProgressDialog();
        addSubscription(apiStores.SaveNickName(params),
                new ApiCallback<GetCodeResult>() {

                    @Override
                    public void onSuccess(GetCodeResult model) {
                        if(model.getStatus()== Config.SUCCESSCODE){
                            SharedPreferences.Editor edit=preferences.edit();
                            edit.putString("nickName",et_nickname.getText().toString());
                            edit.commit();
                            setResult(1);
                            finish();
                        }else{
                            toastShow(model.getMessage());
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        toastShow(msg);

                    }

                    @Override
                    public void onFinish() {
                        dismissProgressDialog();
                    }
                });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bar_btn_back:
                finish();
                break;
            case R.id.tv_bar_edit:
                saveNickName();
                break;
        }
    }
}
